import {createSlice} from "@reduxjs/toolkit";
import axios from "_axios@0.27.2@axios";

const goodsSlice = createSlice({
    name:"goods",
    initialState:{
        goodsList:[],
        info:{}
    },
    reducers:{
        addGoods(state,{payload}){
            state.goodsList.unshift({
                id:Math.random().toString(36).slice(2),
                ...payload
            });
        },
        upInfo(state,{payload}){
            state.info = payload;
        }
    }
})
export const {addGoods,upInfo} = goodsSlice.actions;
export function getInfoAsync(id){
    return function(dispatch){
        axios.get("/info/"+id).then(value=>{
            dispatch(upInfo(value.data));
        })
    }
}
export default goodsSlice.reducer;